package com.ganbo.netty;

import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;

/**
 * netty 根据redis协议连接redis
 *
 * @author ganbo
 * @date 2023/1/2 20:34
 */
public class ConnectRedis {

    public static void main(String[] args) throws InterruptedException {
        byte[] LINE = {13, 10};  //回撤换行
        NioEventLoopGroup worker = new NioEventLoopGroup();

        new Bootstrap()
                .channel(NioSocketChannel.class)
                .group(worker)
                .handler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel ch) throws Exception {
                        ch.pipeline()
                                .addLast(new LoggingHandler(LogLevel.DEBUG))
                                .addLast(new ChannelInboundHandlerAdapter() {
                                    @Override
                                    public void channelActive(ChannelHandlerContext ctx) throws Exception {
                                        ByteBuf buffer = ctx.alloc().buffer();
                                        buffer.writeBytes("*3".getBytes());
                                        buffer.writeBytes(LINE);
                                        buffer.writeBytes("$3".getBytes());
                                        buffer.writeBytes(LINE);
                                        buffer.writeBytes("set".getBytes());
                                        buffer.writeBytes(LINE);
                                        buffer.writeBytes("$4".getBytes());
                                        buffer.writeBytes(LINE);
                                        buffer.writeBytes("name".getBytes());
                                        buffer.writeBytes(LINE);
                                        buffer.writeBytes("$8".getBytes());
                                        buffer.writeBytes(LINE);
                                        buffer.writeBytes("zhangsan".getBytes());
                                        buffer.writeBytes(LINE);
                                        ctx.writeAndFlush(buffer);
                                    }

                                    @Override
                                    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                        ByteBuf buf = (ByteBuf) msg;
                                        final String s = buf.toString();
                                        System.out.println("接收到redis的返回：" + s);
                                    }
                                });
                    }
                }).connect("localhost", 6379).sync();

        System.out.println("连接redis成功。。。");


    }
}
